var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
     conpany:'',
     logo:'',
     license:'',
     idCard:'',
     idCardFront:'',
     idCardBack:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: app.globalData.phpUrl + 'user/api',
      method: 'POST',
      data: {
        api_name: 'company_detail',
        openid: app.globalData.userData.openOId,
      },
      success: function (e) {
        wx.hideLoading();
        console.log(e)
        if (e.data.code == 1) {
          that.setData({
            conpany: e.data.data.company_name,
            logo: e.data.data.logo,
            license: e.data.data.license,
            idCard: e.data.data.idcard,
            idCardFront: e.data.data.idcard_front,
            idCardBack: e.data.data.idcard_back
          });
        } 
      }
    })

  },

  chooseImageLogo: function (e) {
    var that = this;
    

    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        wx.showLoading({
          title: '上传中...',
        });
        wx.uploadFile({
          url: app.globalData.phpUrl + 'Publish/api',
          filePath: res.tempFilePaths[0],
          name: 'img',
          header: {
            "Content-Type": "multipart/form-data",
          },
          formData: {
            'api_name': 'upload',
            'type': 1,
          },
          success: function (res) {
            wx.hideLoading()
            if (JSON.parse(res.data).code == 1){
              that.setData({
                logo: JSON.parse(res.data).data
              });
            }else{
              that.setData({
                logo: ''
              });
              wx.showToast({
                image: "../../../../images/attend.png",
                title: '上传失败!',
                duration: 2000
              })
            }
          }
        });
      }
    })
  },

  clearLogo:function(){
    this.setData({
      logo:''
    })
  },

  chooseImageLicense: function (e) {
    var that = this;
    console.log('上传营业执照')
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        wx.showLoading({
          title: '上传中...',
        });
        wx.uploadFile({
          url: app.globalData.phpUrl + 'Publish/api',
          filePath: res.tempFilePaths[0],
          name: 'img',
          header: {
            "Content-Type": "multipart/form-data",
          },
          formData: {
            'api_name': 'upload',
            'type': 1,
          },
          success: function (res) {
            wx.hideLoading()
            if (JSON.parse(res.data).code == 1) {
              that.setData({
                license: JSON.parse(res.data).data
              });
            } else {
              that.setData({
                license: ''
              });
              wx.showToast({
                image: "../../../../images/attend.png",
                title: '上传失败!',
                duration: 2000
              })
            }
          }
        });
      }
    })
  },

  clearLicense: function () {
    this.setData({
      license: ''
    })
  },

  chooseImageIdCard: function (e) {
    var that = this;
    console.log('手持身份证')
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        wx.showLoading({
          title: '上传中...',
        });
        wx.uploadFile({
          url: app.globalData.phpUrl + 'Publish/api',
          filePath: res.tempFilePaths[0],
          name: 'img',
          header: {
            "Content-Type": "multipart/form-data",
          },
          formData: {
            'api_name': 'upload',
            'type': 1,
          },
          success: function (res) {
            wx.hideLoading()
            if (JSON.parse(res.data).code == 1) {
              that.setData({
                idCard: JSON.parse(res.data).data
              });
            } else {
              that.setData({
                idCard: ''
              });
              wx.showToast({
                image: "../../../../images/attend.png",
                title: '上传失败!',
                duration: 2000
              })
            }
          }
        });
      }
    })
  },


  clearIdCard: function () {
    this.setData({
      idCard: ''
    })
  },


  chooseImageIdCardFront: function (e) {
    var that = this;
    console.log('上传正面身份证')
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        wx.showLoading({
          title: '上传中...',
        });
        wx.uploadFile({
          url: app.globalData.phpUrl + 'Publish/api',
          filePath: res.tempFilePaths[0],
          name: 'img',
          header: {
            "Content-Type": "multipart/form-data",
          },
          formData: {
            'api_name': 'upload',
            'type': 1,
          },
          success: function (res) {
            wx.hideLoading()
            if (JSON.parse(res.data).code == 1) {
              that.setData({
                idCardFront: JSON.parse(res.data).data
              });
            } else {
              that.setData({
                idCardFront: ''
              });
              wx.showToast({
                image: "../../../../images/attend.png",
                title: '上传失败!',
                duration: 2000
              })
            }
          }
        });
      }
    })
  },

  clearIdCardFront: function () {
    this.setData({
      idCardFront: ''
    })
  },


  chooseImageIdCardBack: function (e) {
    var that = this;
    console.log('上传背面身份证')
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        wx.showLoading({
          title: '上传中...',
        });
        wx.uploadFile({
          url: app.globalData.phpUrl + 'Publish/api',
          filePath: res.tempFilePaths[0],
          name: 'img',
          header: {
            "Content-Type": "multipart/form-data",
          },
          formData: {
            'api_name': 'upload',
            'type': 1,
          },
          success: function (res) {
            wx.hideLoading()
            if (JSON.parse(res.data).code == 1) {
              that.setData({
                idCardBack: JSON.parse(res.data).data
              });
            } else {
              that.setData({
                idCardBack: ''
              });
              wx.showToast({
                image: "../../../../images/attend.png",
                title: '上传失败!',
                duration: 2000
              })
            }
          }
        });
      }
    })
  },

  clearIdCardBack: function () {
    this.setData({
      idCardBack: ''
    })
  },


  inputChange:function(e){
    this.setData({
      conpany: e.detail.value
    })
  },

  success:function(){
       var that = this;
       if (!that.data.conpany){
         wx.showToast({
           image: "../../../../images/attend.png",
           title: '请输入企业名称',
           duration: 2000
         })
         return 
       };
       if (!that.data.logo) {
         wx.showToast({
           image: "../../../../images/attend.png",
           title: '请上传企业logo',
           duration: 2000
         })
         return
       }
       if (!that.data.license) {
         wx.showToast({
           image: "../../../../images/attend.png",
           title: '请上传营业执照',
           duration: 2000
         })
         return
       };
       if (!that.data.idCard) {
         wx.showToast({
           image: "../../../../images/attend.png",
           title: '请上传身份证',
           duration: 2000
         })
         return
       };
       if (!that.data.idCardFront) {
         wx.showToast({
           image: "../../../../images/attend.png",
           title: '请上传身份证正面',
           duration: 2000
         })
         return
       };

       if (!that.data.idCardBack) {
         wx.showToast({
           image: "../../../../images/attend.png",
           title: '请上传身份证反面',
           duration: 2000
         })
         return
       };

       
       wx.showLoading({
         title: '上传中...',
       });


       var img1 = that.data.logo;
       var img2 = that.data.license;
       var img3 = that.data.idCard;
       var img4 = that.data.idCardFront;
       var img5 = that.data.idCardBack;

        wx.request({
          url: app.globalData.phpUrl + 'user/api',
          method: 'POST',
          data: {
            api_name: 'company_ident',
            openid: app.globalData.userData.openOId,
            company_name: that.data.conpany,
            logo: img1,
            license: img2,
            idcard: img3,
            idcard_front: img4,
            idcard_back: img5
          },
          success: function (e) {
            wx.hideLoading();
            if (e.data.code == 1) {
              wx.showToast({
                title: '提交成功!',
                duration: 4000
              })
            } else {
              wx.showToast({
                image: "../../../../images/attend.png",
                title: '提交失败!',
                duration: 2000
              })
            }
          }
        })
       
       
  }

})